package com.example.service;

import com.example.dao.Role;
import com.example.dao.User;
import com.example.dao.UserRole;
import com.example.dao.mapper.RoleMapper;
import com.example.dao.mapper.RolePermissionMapper;
import com.example.dao.mapper.UserRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 功能描述
 * 角色service
 * @author: jimmy
 * @date: 2023年12月22日 11:19
 */
@Service
public class RoleService {
    /**
     * 角色mapper
     */
    @Autowired
    private RoleMapper roleMapper;

    /**
     * 用户角色mapper
     */
    @Autowired
    private UserRoleMapper userRoleMapper;

    /**
     * 角色权限mapper
     */
    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    /**
     * 根据用户id查询角色
     * @param user
     * @return
     */
    public List<Role> findRolesByUser(User user) {
        return roleMapper.findRolesByUser(user);
    }

    /**
     * 根据相关条件查询角色总数
     * @param role
     * @return
     */
    public Integer findCount(Role role) {
        return roleMapper.findCount(role);
    }

    /**
     * 根据相关条件分页查询
     * @param role
     * @return
     */
    public List<Role> findRolesByPage(Role role) {
        return roleMapper.findUsersByPage(role);
    }

    /**
     * 根据角色id删除角色及相关数据
     * @param ids
     * @return
     */
    @Transactional
    public int deleteRoleByIds(Long[] ids) {
        int result = 0;
        List<Long> list = Arrays.asList(ids);
        for (Long id : list) {
            Map<String, Object> delMap = new HashMap<>();
            delMap.put("role_id", id);
            //删除用户角色
            result += userRoleMapper.deleteByMap(delMap);
            //删除角色权限
            result +=rolePermissionMapper.deleteByMap(delMap);
        }
        //删除角色
        result += roleMapper.deleteBatchIds(list);
        return result;
    }

    /**
     * 添加角色
     * @param role
     * @return
     */
    public int insertRole(Role role) {
        return roleMapper.insert(role);
    }

    /**
     * 更新角色
     * @param role
     * @return
     */
    public int updateRole(Role role) {
        return roleMapper.updateById(role);
    }

    /**
     * 检查角色名是否存在
     * @param rolename
     * @param id
     * @return
     */
    public boolean isRolenameExisted(String rolename, Long id) {
        Map<String, Object> param = new HashMap<>();
        param.put("rolename", rolename);
        param.put("id", id);
        return roleMapper.findCountByRolename(param) > 0;
    }

    /**
     * 根据用户id查询分配给该用户的角色id，
     * @param userId
     * @return
     */
    public List<Long> findRoleIdsByUserId(Long userId) {
        return roleMapper.findRoleIdsByUserId(userId);
    }

    /**
     * 设置用户角色
     *
     * @param addData
     * @param delData
     * @param userId
     * @return
     */
    @Transactional
    public int attachRoleToUser(Long[] addData, Long[] delData, Long userId) {
        int result = 0;
        //删除用户角色
        if (delData != null) {
            for (Long roleId : delData) {
                UserRole userRole = new UserRole();
                userRole.setUserId(userId);
                userRole.setRoleId(roleId);
                result += userRoleMapper.deleteUserRoleByBothId(userRole);
            }
        }
        //新增用户角色
        if (addData != null) {
            for (Long roleId : addData) {
                UserRole userRole = new UserRole();
                userRole.setUserId(userId);
                userRole.setRoleId(roleId);
                result += userRoleMapper.insert(userRole);
            }
        }
        return result;
    }

//    public List<Role> findAllRole() {
//        return roleMapper.findAllRole();
//    }

    /**
     * 根据角色id查询总数，检查该角色是否分配给用户
     * @param ids
     * @return
     */
    public int findUserCountByRoleIds(Long[] ids) {
        int result = 0;
        for (Long id : ids) {
            result += userRoleMapper.findUserCountByRoleId(id);
        }
        return result;
    }

}
